﻿Imports System.SDX9

Public Class Form1
    Dim dx As Core3D

    '
    ' Ported by Fabio Guerrazzi - unpluggedmail@gmail.com 
    ' 


    Sub Render()
        If dx Is Nothing Then Stop
        Do While dx.Active
            dx.DrawScene()
            Application.DoEvents()
        Loop
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If dx IsNot Nothing Then dx.Dispose()

    End Sub
    
    Private Sub btEnd_Click(sender As Object, e As EventArgs) Handles btEnd.Click
        Try

            If dx IsNot Nothing Then
                dx.Terminate()
                dx.Dispose()
            End If
            CtlSceneTree1.Dispose()
            End
        Catch ex As Exception

        End Try

    End Sub

   

    Private Sub Pic_MouseDown(sender As Object, e As MouseEventArgs) Handles Pic.MouseDown
        If dx IsNot Nothing Then dx.Window_MouseDown(sender, e)
    End Sub

    Private Sub Pic_MouseMove(sender As Object, e As MouseEventArgs) Handles Pic.MouseMove
        If dx IsNot Nothing Then dx.Window_MouseMove(sender, e)
        Text = e.X & " ; " & e.Y
    End Sub

    Private Sub Pic_MouseUp(sender As Object, e As MouseEventArgs) Handles Pic.MouseUp
        If dx IsNot Nothing Then dx.Window_MouseUp(sender, e)
    End Sub


    Sub AddSimpleScene()

        With dx ' 
            .Pause = True ' <--- Stop rendering while is loading objects

            .BackColor = QBColor(8) 'Pic.BackColor
            'BackgroundImage = App.Path & "\casa.jpg"
            '.SetBackGroundImage BackgroundImage

            .SetCameraView(0, 0, -50)


            ' Objects/Scene Construction..
            ' few methods to build almost all :/
            ' look the structure in this way:
            '  Frames are the object you can move and rotate in the scene
            '  Meshes are objects added to single frames
            '   so
            '    I add a frame called "Frame1" where next i add two mesh objects "m1" and "m2"
            '    so far, the structure could be view like an object tree where i define all object
            '    by knot moving them

            ' AddNewFrame:                                    ' Add a new frame to the scene
            ' Addxxx <framename>,<meshname>, <parameters>     ' add a mesh to the frame object
            ' Mesh_AddMaterial <meshName>, <material_count>, _
            '                  <Ambient color>,<Diffuse color>,<specular color> _
            '                  <power 0-255>,<optional 1=sets as transparent object>
            '                  ' apply a material/color to the mesh
            ' frame_Translate  <posxyz) ' translate frame


            ' setup 2 separate containers
            .newFrame("Scene1")
            .newFrame("Scene2")
            ' 
            .NewChildFrame("Scene1", "Plane1")
            .AddBox("Plane1", "Box1", 10, 10, 1)
            .Frame_Translate("Plane1", 0, -1, 0)
            .Mesh_AddMaterial("Box1", 0, QBColor(6), &H6FC0C0C0, QBColor(15), 0.5)

            If 1 = 1 Then



                .NewChildFrame("Scene1", "Cube1") ' Add new frame as cube
                .AddBox("Cube1", "MeshCube", 4, 4, 4)
                .Frame_Translate("Cube1", 0, 1, 0)
                .Mesh_AddMaterial("MeshCube", 0, QBColor(1), &H6FC0C0C0, QBColor(3), 1)


                '   .NewChildFrame("Scene2", "Sphere1") ' Add new frame as sphere
                .AddShapeSphere("Scene2", "sfera1", 3, 16, 16)
                .Frame_Translate("Scene2", 0, 8, 0)
                .Mesh_AddMaterial("sfera1", 0, QBColor(6), &H6FC0C0C0, QBColor(15), 1, 1)
                '  .Mesh_AddTexture("sfera1", 0, "C:\0\dx\Cad-3D\bricks.dds")

            End If


            '  .SetGlobalEffect("C:\0\Dx\Cad-3D\diffuse.fx")

            '  .Frame_Select("Sphere1") = True

            ' .Mesh_AddTexture "sfera1", 0, "E:\Dx8SDK\samples\Multimedia\VBSamples\Media\texr1.bmp"
            '  .Mesh_AddTexture "Box1", 0, "E:\Dx8SDK\samples\Multimedia\VBSamples\Media\dx_logo.bmp"

            '   .SetGlobalEffect("C:\0\Dx\Cad-3D\diffuse.fx")


            ' Here an extruded object using the xMesh modeler class
            ' just add the 2D contour and calls the Extrude method with the height value
            ' Next, it is treat as a normal mesh object and we can apply material or textures
            ' Note: at the moment the textures are applyed only by faces
            '

            ' this below doesn works
            ' wwas working the mesh builder but cant apply textures without apssing from effects.. no way.. too wasted time.

            '.NewChildFrame("Scene2", "user1")
            '.xMesh = New MeshBuilder
            '.xMesh.MeshType = MeshBuilder.EnumMesh.Primitive

            '.xMesh.AddProfileVertex(0, 0)
            '.xMesh.AddProfileVertex(0, 5)
            ''      .xMesh.AddProfileVertex(3, 2)
            '.xMesh.AddProfileVertex(5, 5)
            '.xMesh.AddProfileVertex(5, 0)
            ''   .xMesh.AddProfileVertex(0, 0)
            '.xMesh.Extrude2(10)
            '.AddUserMesh("user1", "myMesh1", .xMesh)
            ''.xMesh.Init()
            '.Frame_Translate("user1", -8, 0, 10)
            '.Mesh_AddMaterial("myMesh1", 0, QBColor(12), &H6FC0C0C0, QBColor(12), 0.5)
            '.Mesh_AddTexture("myMesh1", 0, "c:\0\fabio-duotone.jpg", 1)

            ' ================================================================

            '  .NewChildFrame("Scene2", "testing")
            '    .xMesh = New MeshBuilder
            '    .xMesh.MeshType = MeshBuilder.EnumMesh.Teapot
            '    .AddUserMesh("testing", "teapot", .xMesh)
            '   .Frame_Translate("testing", -8, 1, 0)
            '   .Mesh_AddMaterial("testing", 0, QBColor(12), &H6FC0C0C0, QBColor(1), 150)

            '' uncomment these lines typing any valid bitmap

            ''    .Mesh_AddTexture "myMesh1", 0, "c:\texr1.bmp" '"E:\Dx8SDK\samples\Multimedia\Media\env3.bmp"
            ''    .Mesh_AddTexture "Box1", 0, "E:\Dx8SDK\samples\Multimedia\VBSamples\Media\dx_logo.bmp", 1

            '.NewChildFrame("Scene2", "Poly1")
            '.AddPolygon("Poly1", "MyPoly", 30, 4)
            '.Mesh_AddMaterial("MyPoly", 0, QBColor(14), &H6FC0C0C0, QBColor(12), 0, 1)
            ' ====== a QUI  FG 2014 
            .RefreshTree()

            .Pause = False
        End With
    End Sub

     
    Private Sub AddCubeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AddCubeToolStripMenuItem.Click
        AddSimpleScene()
    End Sub

    Private Sub LoadFromFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoadFromFileToolStripMenuItem.Click
        dx.Pause = True
        With OpenFileDialog1
            .FileName = ""
            .Filter = "x File|*.x"
            .ShowDialog()
            InsertXFile(.FileName)
            dx.RefreshTree()
            dx.SetCameraAutoOptimalView()
        End With
        dx.Pause = False
    End Sub

    Sub InsertXFile(File As String)
        Dim Scene As String, Frame As String
        If System.IO.File.Exists(File) = False Then Return


        With dx

            '  .BackColor = QBColor(8) 'Pic.BackColor
            .SetCameraView(0, 5, -50)
            .Pause = True
            Scene = "S:" & .GenerateHandle
            Frame = "X:" & .GenerateHandle
            .newFrame(Scene)
            .LoadXFile(File, Frame, Scene)
            .RefreshTree()
            .Pause = False

        End With

    End Sub

    Private Sub BackgroundColorToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BackgroundColorToolStripMenuItem.Click
        If dx Is Nothing Then Return
        dx.Pause = True

        With ColorDialog1
            .ShowDialog()
            dx.BackColor = .Color.ToArgb
        End With
        dx.Pause = False
    End Sub

  

    Private Sub ZoomExtentsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ZoomExtentsToolStripMenuItem.Click
        dx.CenterScene()
    End Sub

    Private Sub CtlSceneTree1_Click(sender As Object, e As EventArgs) Handles CtlSceneTree1.Click

    End Sub

    Sub Tree_Click(node As TreeNode)
        MsgBox(node.Text)
        Return
        Static bval As String
        If bval <> CtlSceneTree1.SelectedNode Then
            bval = CtlSceneTree1.SelectedNode
            SelectItem(bval)
        End If
    End Sub
    
    
    Private Sub CtlSceneTree1_Invalidated(sender As Object, e As InvalidateEventArgs) Handles CtlSceneTree1.Invalidated
        AddHandler CtlSceneTree1.Tree_Click, AddressOf Tree_Click
    End Sub

    Sub SelectItem(nam As String)


        dx.Frame_Select(nam) = Not dx.Frame_Select(nam)


    End Sub

   
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Cursor = Cursors.WaitCursor

        dx = New Core3D(Pic)
        ' .InitMode:  0-ask for device/window or fullscreen, 1-Windowed, 2-FullScreen
        dx.InitMode = 1

        If Not dx.InitDevice Then
            Cursor = Cursors.Default
            MsgBox("Unable to CreateDevice.") ' no video card available
            End
        End If

        Select Case dx.DeviceMode ' here i get which InitDevice found as best mode
            Case Core3D.DeviceModeType.Reference : Text = "Accelleration Mode: Medium (D3DDEVTYPE_REF) "
            Case Core3D.DeviceModeType.Software : Text = "Accelleration Mode: Low! (D3DDEVTYPE_SW) "
            Case Core3D.DeviceModeType.NativeHardwareAccelerated : Text = "Accelleration Mode: Best (D3DDEVTYPE_HAL)"
        End Select


        dx.MediaPath = My.Application.Info.DirectoryPath & "\Media\" ' sets the media folder where to get all x files and textures
        dx.BackColor = QBColor(8) 'Pic.BackColor
        dx.SetCameraView(0, 0, -50)

        Timer1.Enabled = False

        System.Threading.Thread.Sleep(2000)

        Cursor = Cursors.Default
        Render()


    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        If dx IsNot Nothing Then
            dx.Window_Resize(Pic)
        End If
    End Sub
End Class
